<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
 
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
sql: <input type="text" name="sql">
<input type="submit" value="提交">
</form>
<?php
echo $_POST["sql"] . "</br>";
?>
<?php

//PHP查询MySql数据库，将结果用表格输出

$con = mysql_connect("localhost", "root", "root");
if (!$con) {
    die('Could not connect: ' . mysql_error());
}
mysql_select_db("test", $con);
$sql = $_POST["sql"];
$res = mysql_query($sql, $con);
$rows = mysql_affected_rows($con);//获取行数
$colums = mysql_num_fields($res);//获取列数
        //echo "test数据库的"."$table_name"."表的所有用户数据如下：<br/>";
echo "共计" . $rows . "行 " . $colums . "列<br/>";
echo '<table  border="1"><tr>';
        //for($i=0; $i < 100; $i++){
for ($i = 0; ($i < $colums) and ($i < 100); $i++) {
    $field_name = mysql_field_name($res, $i);
    echo "<th>$field_name</th>";
}
echo "</tr>";
while ($row = mysql_fetch_row($res)) {
    echo "<tr>";
    for ($i = 0; $i < $colums; $i++) {
        echo "<td>$row[$i]</td>";
    }
    echo "</tr>";
}
echo "</table>";
?>
</body>
</html>